package com.liwei.toc.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

public class getWebContent {
    public static String getContent(String myurl, String Ecode) {

        String s = "";
        StringBuffer sb = new StringBuffer("");

        try {
            URL urlmy = new URL(myurl);
            HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
            con.setFollowRedirects(true);
            con.setInstanceFollowRedirects(false);
            con.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),
                    Ecode));

            while ((s = br.readLine()) != null) {

                sb.append(s + "\r\n");
            }
        } catch (Exception e) {
            System.out.println(e.toString());
            return "ConnectException";
        }

        return sb.toString();
    }

    public static void writeFileByChars(String fileName, String content) {
        File file = new File(fileName);
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(file));
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e1) {
                }
            }
        }
    }

}
